﻿using BusinessLayer;
using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using System.Collections.Generic;
using System.Web.Http;
using TrialManagementTool.App_Start;
using TrialManagementTool.Helper;
using TrialManagementTool.Models;

namespace TrialManagementTool.Controllers.API
{
    [WebApiExceptionFilter]
    [APIAuthorize]
    public class PromoterController : ApiController
    {
        public List<PromoterCityMapping> GetAllPromoterCity()
        {
            VPromoterProvinceMappings[] PromoterInfo = BusinessLayer.PromoterBusiness.GetPromoterProvincMappings("");
            List<PromoterCityMapping> result = new List<PromoterCityMapping>();
            PromoterCityMapping mapping = new PromoterCityMapping();

            foreach (VPromoterProvinceMappings item in PromoterInfo)
            {
                mapping = new PromoterCityMapping();
                mapping.ChineseName = item.Promoter.ChineseName;
                foreach (VDataEntity data in item.Provinces)
                {
                    mapping.Citys = string.Format("{0}{1}&nbsp;&nbsp;&nbsp;&nbsp", mapping.Citys, data.Name);
                }
                mapping.ID = item.Promoter.ID;
                mapping.IsActive = item.Promoter.IsActive;
                result.Add(mapping);
            }
            return result;
        }

        public void DelCityMapping(PromoterCityMapping mapping)
        {
            BusinessLayer.PromoterBusiness.DeleteLocationToPromoter(mapping.ID, mapping.CityID);
        }

        public void AddPromoter(Promoter promoter)
        {
            BusinessLayer.PromoterBusiness.AddPromoter(promoter.ChineseName, promoter.UserName);
        }

        [HttpGet]
        public void AddCityToPromoter(int cityID, int PromterID)
        {
            BusinessLayer.PromoterBusiness.AddLocationToPromoter(PromterID, cityID);
        }

        public VDataEntity[] GetProvinceByPromoterId()
        {
           return  PromoterBusiness.GetProvinceMappingsByPromoterID(UserHelper.GetCurrentPromoterUser().ID).Provinces;
        }

        [HttpPost]
        public void UpdatePromoterStatus(VPromoter promoter)
        {
            PromoterBusiness.PromoterStatusUpdate(promoter.ID);
        }
    }
}
